// const db = require('../config/config')
const { dbMysql } = require('../../config/config');
const db = require('../../config/config')

module.exports = {
  //获取分类信息
  getCategory (req, res) {
    db.dbMysql('select categoryId,categoryName as name from category', [], function (err, data) {
      if (data) {
        res.send({
          code: 200,
          data: data,
          msg: '查询成功'
        })
      } else {
        res.send({
          code: 500,
          msg: '服务器错误'
        })
      }
    })
  },
  //根据分类获取对应商品
  getProduct (req, res) {
    console.log(req.body);
    let sql
    if (req.body) {
      sql = `select productId,productName,productPrice,pImg,likeNum,collectNum,shopId,shopName from product,category,shop where proCategoryId=categoryId and shopId=productShopId and proCategoryId=${req.body.categoryId}`
    } else {
      sql = `select productId,productName,productPrice,pImg,likeNum,collectNum,shopId,shopName from product,shop where shopId=productShopId`
    }
    console.log(sql);
    db.dbMysql(sql, [], function (err, data) {
      if (data) {
        if (data.length == 0) {
          res.send({
            code: 201,
            msg: '暂无数据'
          })
        } else {
          res.send({
            code: 200,
            data: data,
            msg: '查询成功'
          })
        }
      } else {
        console.log(err);
      }
    })
  },

}